package org.example.car.system.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import org.example.car.system.config.R;
import org.example.car.system.domain.Role;
import org.example.car.system.domain.SysRole;
import org.example.car.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("list")
    public R list(@RequestBody SysRole sysRole){
        System.out.println(sysRole);
        PageInfo<SysRole> pageInfo = sysRoleService.selectAlls(sysRole);
        return R.successed(pageInfo);
    }

    //角色列表
    @RequestMapping("/roleList")
    public List<SysRole> roleList(){
        List<SysRole> list = sysRoleService.selectAll();
        return list;
    }

    @RequestMapping("down")//导出下载
    public void down(HttpServletResponse response) throws IOException {
        List<SysRole> list = sysRoleService.list();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName= URLEncoder.encode("角色导出测试","utf-8").replaceAll("\\+","%20");
        response.setHeader("content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(), SysRole.class).sheet("角色模版").doWrite(list);
    }
//    @RequestMapping("upload")//批量导入数据
//    public Map<String,Object> upload(MultipartFile file){
//        Map<String,Object> map = new HashMap<>();
//        try {
//            map.put("code",200);
//            EasyExcel.read(file.getInputStream(), SysRole.class, new SysRoleListener(sysRoleService)).sheet().doRead();
//        }catch (IOException e){
//            map.put("code",201);
//            e.printStackTrace();
//        }
//        return map;
//    }
    @RequestMapping("add")
    public Map<String,Object> add(@RequestBody SysRole sysRole){
        Map<String,Object> map = new HashMap<>();
        Boolean absent = redisTemplate.opsForValue().setIfAbsent(sysRole.toString(), sysRole, 1, TimeUnit.HOURS);
        if (absent){
            map.put("msg","请勿重复提交");
        }
        sysRoleService.saves(sysRole);
        map.put("code",1001);
        return map;
    }
    @RequestMapping("update")
    public void aa(@RequestBody SysRole sysRole){
        sysRoleService.updates(sysRole);
    }
    @RequestMapping("rs")
    public R rs(Integer id){
        sysRoleService.updateRs(id);
        return R.successed(null);
    }
    @RequestMapping("del")
    public void sc(String ids){
        List<String> asList = Arrays.asList(ids.split(","));
        sysRoleService.removeBatchByIds(asList);
    }


//    @RequestMapping("excel")
//    public void excel(HttpServletResponse response) throws IOException {
//        List<SysRole> list = sysRoleService.list();
//        ExcelDownloader.download(response,list, SysRole.class,"导出");
//        System.out.println("导出成功");
//    }


}
